package com.ruibei.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import com.ruibei.R;
import com.ruibei.models.ListItemModel;
import com.ruibei.ui.NewsDetailActivity;
import com.ruibei.ui.adapter.ListItemAdapter;
import com.ruibei.utils.TaskUtils;
import com.ruibei.view.EmptyLayout;
import com.ruibei.view.TitleBarView;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 消息页面
 *
 * @author limingyan
 *	
 */
public class NewsFragment extends BaseFragment{
	@InjectView(R.id.news_list) 
	ListView news_list;
	//标题栏
	private TitleBarView mTitleBarView;
	private View mBaseView;
	//适配器
	private ListItemAdapter madapter;
	//展现不同的layout
	//private EmptyLayout mEmptyLayout;
	
	private List<ListItemModel> news_list_data=new ArrayList<ListItemModel>();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mBaseView = inflater.inflate(R.layout.fragment_news, container,
				false);
		ButterKnife.inject(this, mBaseView);
		//mEmptyLayout = new EmptyLayout(getActivity(), news_list);
		initTitleView();
		return mBaseView;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//mEmptyLayout.showLoading();
		//mEmptyLayout.showError();
		//mEmptyLayout.showEmpty();
	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	private void initTitleView(){
		mTitleBarView = (TitleBarView) mBaseView.findViewById(R.id.news_title_bar);
		mTitleBarView.setCommonTitle(View.GONE, View.VISIBLE, View.GONE);
		mTitleBarView.setTitleText(R.string.news_title);
		loadData();
	}
	
	private void loadData() {
		
		ListItemModel listItemModel = new ListItemModel();
		for (int i = 0; i < 5; i++) {
			listItemModel.setContent("审批内容审批内容审批内容");
			listItemModel.setTitle("销售指导价格审批");
			listItemModel.setDate("昨天 上午9:30");
			listItemModel.setApproval_reason("审批原因审批原因");
			news_list_data.add(listItemModel);
		}
		
		madapter=new ListItemAdapter(getActivity(), news_list_data);
 		news_list.setAdapter(madapter);
 		news_list.setOnItemClickListener(newsListItemClickEvent);
		
//		TaskUtils.executeAsyncTask(new AsyncTask<Object, Object, Object>() {
//            @Override
//            protected Object doInBackground(Object... params) {
////            	try {
////					Thread.sleep(100);
////				} catch (InterruptedException e) {
////					// TODO Auto-generated catch block
////					e.printStackTrace();
////				}
//            	ListItemModel listItemModel = new ListItemModel();
//        		for (int i = 0; i < 5; i++) {
//        			listItemModel.setContent("审批内容审批内容审批内容");
//        			listItemModel.setTitle("销售指导价格审批");
//        			listItemModel.setDate("昨天 上午9:30");
//        			listItemModel.setApproval_reason("审批原因审批原因");
//        			news_list_data.add(listItemModel);
//        		}
//            	return null;
//            }
//
//            @Override
//            protected void onPostExecute(Object o) {
//                super.onPostExecute(o);
//                madapter=new ListItemAdapter(getActivity(), news_list_data);
//        		news_list.setAdapter(madapter);
//        		news_list.setOnItemClickListener(newsListItemClickEvent);
//            }
//        });
	}
	
	private OnItemClickListener newsListItemClickEvent=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(NewsFragment.this.getActivity(),NewsDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("itemModel", news_list_data.get(arg2));
			intent.putExtras(bundle);
			startActivity(intent);
		}
	};
	
}
